/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.business.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.oss.OSSClient;
import com.bm.console.business.WebCityService;
import com.bm.console.common.OSSConfig;
import com.bm.console.pojo.WebCity;
import com.bm.console.pojo.WebCityItem;
import com.bm.console.pojo.WebCityPhoto;
import com.bm.console.pojo.WebHousePhoto;
import com.bm.console.pojo.mapper.WebCityMapper;
import com.bm.console.pojo.mapper.WebCityPhotoMapper;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.page.PageBean;
import com.sys.object.common.utils.Base64Helper;
import com.sys.object.common.utils.OSSUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WebCityServiceImpl implements WebCityService {

    @Autowired
    WebCityMapper webCityMapper;

    @Autowired
    WebCityPhotoMapper webCityPhotoMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public PageBean queryPageList(WebCity entity, PageBean page) throws Exception {
        int rowCount = webCityMapper.selectByPageCount(entity);
        page.setRowCount(rowCount);
        entity.setOffset(page.getStartRow());
        entity.setLimit(page.getPerpage());
        page.setList(webCityMapper.selectByPage(entity));
        return page;
    }

    public WebCity queryByPrimaryKey(Integer id) throws Exception {
        return webCityMapper.selectByPrimaryKey(id);
    }

    public int saveOrUpdate(WebCity entity) {
        try {
            Sys_User currentUser = (Sys_User) request.getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
            if (!StringUtils.isEmpty(entity.getFiledata()) && !StringUtils.isEmpty(entity.getFileext())) {
                byte[] data = Base64Helper.getImageFromBASE64ToByte(entity.getFiledata());
                String filename = UUID.randomUUID() + "." + entity.getFileext();
                OSSClient ossClient = new OSSClient(OSSConfig.oss_endpoint, OSSConfig.oss_accessKeyId,
                        OSSConfig.oss_accessKeySecret);
                String eTag = OSSUtil.uploadObject2OSS(ossClient, data, filename, OSSConfig.oss_backetName, "photo/");
                ossClient.shutdown();
                if (!StringUtils.isEmpty(eTag)) {
                    entity.setPhoto(OSSConfig.oss_domain + "photo/" + filename);
                }
            }
            List<String> photos_list = new ArrayList<>();
            if(!StringUtils.isEmpty(entity.getPhotos())) {
                String photos = new String(Base64.decodeBase64(entity.getPhotos().getBytes()));
                photos_list = JSON.parseObject(photos, new TypeReference<List<String>>() {
                });
            }
            if (entity.getId() != null && entity.getId() > 0) {

                webCityPhotoMapper.deleteByCityId(entity.getId());
                WebCityPhoto cityPhoto = new WebCityPhoto();
                cityPhoto.setCityId(entity.getId());
                if (photos_list != null && photos_list.size() > 0) {
                    int sort = 1;
                    for (String photo : photos_list) {
                        System.err.println(photo);
                        cityPhoto.setId(null);
                        cityPhoto.setPhoto(photo);
                        cityPhoto.setCreateTime(new Date());
                        cityPhoto.setStatus(true);
                        cityPhoto.setSort(sort);
                        webCityPhotoMapper.insert(cityPhoto);
                        sort++;
                    }
                }

                entity.setUpdateTime(new Date());
                entity.setUpdateId(Long.valueOf(currentUser.getUid()));
                return this.webCityMapper.updateByPrimaryKeySelective(entity);
            } else {
                entity.setCreateTime(new Date());
                entity.setCreateId(Long.valueOf(currentUser.getUid()));

                int insert = this.webCityMapper.insert(entity);

                //webCityPhotoMapper.deleteByCityId(entity.getId());
                WebCityPhoto cityPhoto = new WebCityPhoto();
                cityPhoto.setCityId(entity.getId());
                if (photos_list != null && photos_list.size() > 0) {
                    int sort = 1;
                    for (String photo : photos_list) {
                        System.err.println(photo);
                        cityPhoto.setId(null);
                        cityPhoto.setPhoto(photo);
                        cityPhoto.setCreateTime(new Date());
                        cityPhoto.setStatus(true);
                        cityPhoto.setSort(sort);
                        webCityPhotoMapper.insert(cityPhoto);
                        sort++;
                    }
                }

                return insert;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public int remove(Integer id) throws Exception {
        return this.webCityMapper.deleteByPrimaryKey(id);
    }

    public int delete(Integer id) throws Exception {
        return this.webCityMapper.deleteByPrimaryKey(id);
    }

    public int updateStatus(WebCity entity) throws Exception {
        return this.webCityMapper.updateByPrimaryKeySelective(entity);
    }

    public int updateStatus(String status, String[] ids)
            throws Exception {
        return 0;
    }

    @Override
    public List<WebCityItem> selectAll() {
        return webCityMapper.selectAll();
    }

    @Override
    public List<WebCityItem> selectByCountryId(Long countryId) {
        return webCityMapper.selectByCountryId(countryId);
    }
}
